package com.ssfeng.youxia.config;

import com.ssfeng.youxia.services.EnvironmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
	
	@Autowired
	private EnvironmentService environmentService;
	
	/*@Bean
	public JedisPoolConfig  jedisPoolConfig() {
		JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
		jedisPoolConfig.setMaxIdle(Integer.parseInt(environmentService.getProperty("maxIdle")));
		jedisPoolConfig.setMinIdle(Integer.parseInt(environmentService.getProperty("minIdle")));
		jedisPoolConfig.setTestOnBorrow(Boolean.valueOf(environmentService.getProperty("testOnBorrow")));
		return jedisPoolConfig;
	}*/
	
	/*@Bean
	public JedisPool jedisPool(JedisPoolConfig jedisPoolConfig) {
		JedisPool  jedisPool=new JedisPool(jedisPoolConfig, environmentService.getProperty("host"),
				Integer.parseInt(environmentService.getProperty("port")),
				Integer.parseInt(environmentService.getProperty("timeout")));
		return jedisPool;
	}*/


	/*@Bean
	public JedisConnectionFactory jedisConnectionFactory() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
		jedisConnectionFactory.setHostName("127.0.0.1");
		jedisConnectionFactory.setPort(6379);
		jedisConnectionFactory.setPassword("123456");
		jedisConnectionFactory.setTimeout(15000);
		jedisConnectionFactory.setUsePool(true);
		return jedisConnectionFactory;
	}*/

	@Bean
	public JedisConnectionFactory redisConnectionFactory() {
		RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(environmentService.getProperty("host"),
				Integer.parseInt(environmentService.getProperty("port")));
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config);
		return jedisConnectionFactory;
	}

	@Bean
	public RedisTemplate redisTemplate() {
		RedisTemplate redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisConnectionFactory());
		redisTemplate.setEnableTransactionSupport(true);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		return redisTemplate;
	}
	@Bean
	public StringRedisTemplate stringRedisTemplate() {
		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory());
		stringRedisTemplate.setEnableTransactionSupport(true);
		return stringRedisTemplate;
	}

	@Bean
	public DefaultRedisScript<Long> defaultRedisScript() {
		DefaultRedisScript<Long> defaultRedisScript = new DefaultRedisScript<>();
		defaultRedisScript.setResultType(Long.class);
		defaultRedisScript.setScriptText("if redis.call('get', KEYS[1]) == KEYS[2] then return redis.call('del', KEYS[1]) else return 0 end");
		return defaultRedisScript;
	}
}
